<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <script src="../script/api.js"></script>
    <style>

    </style>
</head>

<body>
    <header id='header' class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
        <a class="aui-pull-left aui-btn" onclick="api.closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="middle">
            生产排料确认
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:0px;">
        <ul class="aui-list aui-form-list">
            <!--  <div class="aui-searchbar" id="search" style="position: relative;"> -->
            <!--  <div class="aui-searchbar-input aui-border-radius"> -->
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">生产线名称</div>
                    <select id="workCenterCode" onchange="doSearch()">
                    </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">工单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请输入工单号" class="aui-input" id="workOrderCode">
                    </div>
                </div>
            </li> -->
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width:150px">计划日期</div>
                    <div class="aui-list-item-input" onclick="timePicker();">
                        <input type="text" placeholder="选择日期" class="aui-input" id="dateTime">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="font-size: 14px;">班次</div>
                    <div class="aui-list-item-input">
                        <select id="shiftCode" placeholder="请选择班次" style="font-size: 14px;">
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="font-size: 14px;">班组</div>
                    <div class="aui-list-item-input">
                        <select id="groupCode" placeholder="请选择班组" style="font-size: 14px;">
                        </select>
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="doSearch()">查询</div>
    </div>
    <ul class="ui-selectee" id="tbodyHtml">



    </ul>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script>
    var relworkcenter = "";
    var relworkcell = "";
    var relMrlId = "";
    var relMrlName = "";
    let currentDate = new Date();
    let year = currentDate.getFullYear(); // 获取当前年份
    let month = (currentDate.getMonth() + 1) < 10 ? "0" + (currentDate.getMonth() + 1) : (currentDate.getMonth() + 1); // 获取当前月份，注意月份从0开始，所以要加1
    let day = currentDate.getDate() < 10 ? "0" + currentDate.getDate() : currentDate.getDate(); // 获取当前日期
    let time = year + '-' + month + '-' + day
    apiready = function () {
        $("#dateTime").val(time);
        var query = api.pageParam.query;
        // getCombox();
        //获取班次下拉框
        doSearchShiftName()
        //获取班组下拉框
        doSearchGroupName()
    }
    function focusFn(ele) {
        $api.attr(ele, 'readonly', 'readonly');
        setTimeout(() => {
            $api.removeAttr(ele, 'readonly');
        }, 200);
    };
    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }
    function doSearchGroupName() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUtilComboxList";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                un: un,
                tablename: "md_labour_group",
                column_name: "group_name",
                column_value: "group_code",
                condition: "is_delete=0 ",
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("groupCode"), html);
            }
        });
    }
    function doSearchShiftName() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUtilComboxList";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                un: un,
                tablename: "md_paraconfig",
                column_name: "paracode",
                column_value: "paraval",
                condition: "paracls = 'shiftClass'",
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("shiftCode"), html);
            }
        });
    }
    //时间选择器
    function timePicker() {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function (ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#dateTime").val(date);
            }
        });
    }
    //查询生产线，班次下拉框
    function getCombox() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getComboxLb";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == "0") {

                    //生产线
                    var lineList = eval(myobj.lineList);

                    createCombox(lineList, "workCenterCode")

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }
    //构造下拉框
    function createCombox(list, id) {

        var html = "<option value=''>--请选择--</option>";
        for (var i = 0; i < list.length; i++) {
            html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
        }
        $api.html($api.byId(id), html);
    }
    //查询
    function doSearch() {
        //光标所在id
        var idSelector = "";
        var dateTime = document.getElementById("dateTime").value;
        var shiftCode = document.getElementById("shiftCode").value;
        var groupCode = document.getElementById("groupCode").value;

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getNestConfirm";
        var data = {
            values: {
                dateTime: dateTime,
                shiftCode: shiftCode,
                groupCode: groupCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            console.log("ret:" + JSON.stringify(ret));
            if (ret) {
                var data = JSON.stringify(ret);

                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<li class='sortable ui-selectee ui-sortable'>";
                        html += "<ul class='aui-list aui-media-list ui-selectee'>";
                        html += "<li class='aui-list-item  ui-selectee'>";
                        html += "<div class='aui-media-list-item-inner ui-selectee'>";
                        html += "<div class='aui-list-item-inner ui-selectee'>";
                        html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>工单号</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].work_order_code + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>生产线编码</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].line_code + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>排料记录号</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].code + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料编码</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].mrl_code + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料名称</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].mrl_name + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>批次号</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].lot_code + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>库位</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].work_cell_code + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>缺陷名称</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].cause_of_deficienfy + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>数量</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].scrap_qty + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>申请时间</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].create_date + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>申请人</div>";
                        html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].create_id + "</div>";
                        html += "<div class='aui-list-item-right aui-btn ui-selectee' " +
                            "onclick='confirmInfo(\"" + list[i].sysid + "\",\"" + list[i].work_order_code + "\",\"" + list[i].mrl_code + "\",\""
                            + list[i].lot_code + "\",\"" + list[i].work_cell_code + "\",\"" + list[i].cause_of_deficienfy + "\",\"" + list[i].scrap_qty
                            + "\");'>确认</div>";
                        html += "<div class='aui-list-item-right aui-btn ui-selectee' " + "onclick='deleteInfo(\"" + list[i].sysid + "\")'>撤销</div>";
                        html += "</div>";
                        html += "</div>";
                        html += "</div>";
                        html += "</li>";
                        html += "</ul>";
                        html += "</li>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);
                    //失败
                } else if (myobj.code == "1") {
                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    function confirmInfo(sysid, workOrderCode, mrlCode, lotCode, workCellCode, causeOfDeficienfy, scrapQty) {
        api.confirm({
            title: '提示',
            msg: "是否确认选择的生产排料信息？",
            buttons: ['确定', '取消']
        }, function (ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                save(sysid, workOrderCode, mrlCode, lotCode, workCellCode, causeOfDeficienfy, scrapQty);
            }
        })
    }
    function deleteInfo(sysid) {
        api.confirm({
            title: '提示',
            msg: "是否删除选择的生产排料信息？",
            buttons: ['确定', '取消']
        }, function (ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                saveDel(sysid);
            }
        })
    }
    function save(sysid, workOrderCode, mrlCode, lotCode, workCellCode, causeOfDeficienfy, scrapQty) {

        if (workOrderCode == "" || workOrderCode == null || workOrderCode == undefined) {
            api.toast({
                msg: '生产排料信息不完善：工单信息不存在！',
                duration: 3000
            });
            return;
        }
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            api.toast({
                msg: '生产排料信息不完善：物料信息不存在！',
                duration: 3000
            });
            return;
        }
        if (lotCode == "" || lotCode == null || lotCode == undefined) {
            api.toast({
                msg: '生产排料信息不完善：批次信息不存在！',
                duration: 3000
            });
            return;
        }
        if (workCellCode == "" || workCellCode == null || workCellCode == undefined) {
            api.toast({
                msg: '生产排料信息不完善：库位信息不存在！',
                duration: 3000
            });
            return;
        }
        if (causeOfDeficienfy == "" || causeOfDeficienfy == null || causeOfDeficienfy == undefined) {
            api.toast({
                msg: '生产排料信息不完善：缺陷信息不存在！',
                duration: 3000
            });
            return;
        }
        if (scrapQty == "" || scrapQty == null || scrapQty == undefined) {
            api.toast({
                msg: '生产排料信息不完善：数量信息不存在！',
                duration: 3000
            });
            return;
        }

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveScrapByWorkOrderConfirm";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                sysid: sysid,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {

                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();

                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });

                    doSearch();
                } else {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });

    }
    function saveDel(sysid) {
        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveScrapByWorkOrderDel";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                sysid: sysid,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {

                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();

                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });

                    doSearch();
                } else {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });

    }
    function test() {
        $api.html($api.byId("tbodyHtml"), "");
    }


    //刷新数据
    function refreshData() {
        var dayS = new Date();
        dayS.setTime(dayS.getTime());
        var mm = (dayS.getMonth() + 1) >= 10 ? (dayS.getMonth() + 1) : "0" + (dayS.getMonth() + 1);
        var dd = dayS.getDate() >= 10 ? dayS.getDate() : "0" + dayS.getDate();
        var start =
            dayS.getFullYear() + "-" + mm + "-" + dd;
        doSearch(start);
    }
</script>

</html>